<?php
/**
 * 註冊頁
 * @author user zhukoo
 *
 */
class Register extends Service {
	
	/**
	 * 默認method
	 * @param array $param
	 */
	public function run($param = ''){
		return ;
	}

	/**
	 * 頭像上傳
	 * @param array $param
	 */
	public function upload($param = ''){
		$uf = ZKF::lib_class("UploadFile");
		$uf->savePath = _UPLOAD_.'/photo/temp/';
		$uf->maxSize  = 3145728 ;
	    $uf->allowExts  = array('jpg', 'gif', 'png', 'jpeg');
	    $uf->thumb = true;
	    //$uf->thumbRemoveOrigin = true;
	    $uf->thumbPath = _UPLOAD_.'/photo/';
	    //$uf->thumbExt = 'jpg';
	    $uf->thumbPrefix = 'm_';
	    $uf->thumbMaxWidth = '250';
	    $uf->thumbMaxHeight = '250';
	    if(!$uf->upload()) {
	        return $uf->getErrorMsg();
	    }else{
	    	$info = $uf->getUploadFileInfo();
	        return $info;
	    }
	}
	
	/**
	 * 表單提交
	 * @param array $param
	 */
	public function form($param = ''){
		if(!empty($param)&&is_array($param)){
			//用戶頭像：檢查是否截圖，否則為縮略圖
			if(!empty($param['x2'])&&!empty($param['y2'])){
			    $attr = array(
			    	'photo'=>isset($param['photo'])?$param['photo']:'',
			    	'x1'=>isset($param['x1'])?$param['x1']:0,
			    	'y1'=>isset($param['y1'])?$param['y1']:0,
			    	'x2'=>isset($param['x2'])?$param['x2']:0,
			    	'y2'=>isset($param['y2'])?$param['y2']:0,
			    	'w'=>60,
			    	'h'=>60,
			    );
			    ZKF::import("Image");
			    Image::imgAreaSelect($attr);
			}else{
				ZKF::import("Image");
				$_photo = isset($param['photo'])?$param['photo']:'';
				Image::thumb($_photo, $_photo, '', 60, 60);
			}
			
			//組裝數組，入庫
			$user = ZKF::model('Zpdo');
			$attr = array(
				'username'=>isset($param['username'])?$param['username']:'',
				'password'=>md5('zkf'.(isset($param['password'])?$param['password']:'')),
				'photo'=>isset($param['photo'])?$param['photo']:'',
				'email'=>isset($param['email'])?$param['email']:'',
				'intro'=>isset($param['intro'])?$param['intro']:'',
				'user_type'=>1
			);
			$_return = $user->table('user')->add($attr);
			ZKF::session('verify',0,0);
			return $_return ? array('info'=>'恭喜你，註冊成功！','status'=>'y') : array('info'=>'sorry，註冊失敗！','status'=>'n');
		}
	}
	
	/**
	 * ajax檢查用戶名
	 * @param string $username
	 * @return array
	 */
	public function check_username($username) {
		$pdo = ZKF::model('Zpdo');
		$_return = $pdo->table('user')->field('id')->where('username="'.$username.'"')->fetchHash();
		//echo $pdo->sql();
		return !empty($_return)?array('info'=>'此用戶名已被註冊！','status'=>'n'):array('info'=>'恭喜你，此用戶名可以註冊！','status'=>'y');
	}
	
}
?>